package com.fxf.srb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxf.common.exception.Assert;
import com.fxf.common.result.ResponseEnum;
import com.fxf.srb.core.enums.BorrowInfoStatusEnum;
import com.fxf.srb.core.enums.UserBindEnum;
import com.fxf.srb.core.mapper.BorrowerMapper;
import com.fxf.srb.core.mapper.IntegralGradeMapper;
import com.fxf.srb.core.mapper.UserInfoMapper;
import com.fxf.srb.core.pojo.entity.BorrowInfo;
import com.fxf.srb.core.mapper.BorrowInfoMapper;
import com.fxf.srb.core.pojo.entity.Borrower;
import com.fxf.srb.core.pojo.entity.IntegralGrade;
import com.fxf.srb.core.pojo.entity.UserInfo;
import com.fxf.srb.core.pojo.vo.BorrowInfoApprovalVO;
import com.fxf.srb.core.pojo.vo.BorrowerDetailVO;
import com.fxf.srb.core.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 借款信息表 服务实现类
 * </p>
 *
 * @author fxf
 * @since 2021-05-26
 */
@Service
public class BorrowInfoServiceImpl extends ServiceImpl<BorrowInfoMapper, BorrowInfo> implements IBorrowInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private IntegralGradeMapper integralGradeMapper;
    @Resource
    private BorrowerMapper borrowerMapper;
    @Resource
    private IDictService iDictService;
    @Resource
    private IBorrowerService borrowerService;
    @Resource
    private ILendService iLendService;

    @Override
    public BigDecimal getBorrowAmount(Long userId) {

        //根据借款人获取积分
        UserInfo userInfo = userInfoMapper.selectById(userId);
        Integer integer = userInfo.getIntegral();

        //根据积分获取可借款额度
        QueryWrapper<IntegralGrade> wrapper = new QueryWrapper<>();
        wrapper.le("integer_end",integer);
        wrapper.ge("integer_start",integer);
        IntegralGrade integralGrade = integralGradeMapper.selectOne(wrapper);
        if(integralGrade == null){
            return new BigDecimal("0");
        }
        BigDecimal borrowAmount = integralGrade.getBorrowAmount();
        return borrowAmount;
    }

    @Override
    public void saveBorrowInfo(BorrowInfo borrowInfo, Long userId) {

        //获取当前用户信息
        UserInfo userInfo = userInfoMapper.selectById(userId);
        //判断用户绑定状态
        Assert.isTrue(userInfo.getBindStatus().intValue() == UserBindEnum.BIND_OK.getStatus().intValue(),
                ResponseEnum.USER_NO_BIND_ERROR);

        //判断用户信息是否审批通过
        Assert.isTrue(userInfo.getBorrowAuthStatus().intValue() == BorrowInfoStatusEnum.CHECK_OK.getStatus().intValue(),
                ResponseEnum.USER_NO_AMOUNT_ERROR);
        //判断借款额度是否足够
        BigDecimal borrowAmount = this.getBorrowAmount(userId);
        Assert.isTrue(borrowInfo.getAmount().doubleValue() <= borrowAmount.doubleValue(),
                ResponseEnum.USER_AMOUNT_LESS_ERROR);

        //存储数据
        borrowInfo.setUserId(userId);
        //百分比转成小数
        borrowInfo.setBorrowYearRate( borrowInfo.getBorrowYearRate().divide(new BigDecimal(100)));
        borrowInfo.setStatus(BorrowInfoStatusEnum.CHECK_RUN.getStatus());
        baseMapper.insert(borrowInfo);
    }

    @Override
    public Integer getStatusByUserId(Long userId) {

        BorrowInfo borrowInfo = baseMapper.selectById(userId);
        if (borrowInfo == null){
            return BorrowInfoStatusEnum.NO_AUTH.getStatus();
        }
//        Integer status = borrowInfo.getStatus();

//        QueryWrapper<BorrowInfo> wrapper = new QueryWrapper<>();
//        wrapper.select("status").eq("user_id", userId);
//        List<Object> objects = baseMapper.selectObjs(wrapper);
        return borrowInfo.getStatus();
    }

    @Override
    public List<BorrowInfo> selectList() {
//        QueryWrapper<BorrowInfo> wrapper = new QueryWrapper<>();
//        List<BorrowInfo> borrowInfoList = baseMapper.selectList(wrapper);

//        borrowInfoList.forEach(borrowInfo -> {
//
//        });
        List<BorrowInfo> borrowInfoList = borrowerMapper.selectBorrowInfoList();
        borrowInfoList.forEach(borrowInfo -> {
            //数据转换
            String returnMethod = iDictService.getNameByParentDictCodeAndValue("returnMethod", borrowInfo.getReturnMethod());
            String msgByStatus = BorrowInfoStatusEnum.getMsgByStatus(borrowInfo.getStatus());
            String moneyUse = iDictService.getNameByParentDictCodeAndValue("moneyUse", borrowInfo.getMoneyUse());

            borrowInfo.getParam().put("returnMethod",returnMethod);
            borrowInfo.getParam().put("msgByStatus",msgByStatus);
            borrowInfo.getParam().put("moneyUse",moneyUse);
        });
        return borrowInfoList;
    }

    @Override
    public Map<String, Object> getBorrowInfoDetail(Long id) {
        Map<String, Object> map = new HashMap<>();
        BorrowInfo borrowInfo = baseMapper.selectById(id);

        //数据转换
        String returnMethod = iDictService.getNameByParentDictCodeAndValue("returnMethod", borrowInfo.getReturnMethod());
        String moneyUse = iDictService.getNameByParentDictCodeAndValue("moneyUse", borrowInfo.getMoneyUse());
        String msgByStatus = BorrowInfoStatusEnum.getMsgByStatus(borrowInfo.getStatus());

        borrowInfo.getParam().put("returnMethod",returnMethod);
        borrowInfo.getParam().put("msgByStatus",msgByStatus);
        borrowInfo.getParam().put("moneyUse",moneyUse);

        map.put("borrowInfo",borrowInfo);

        Long userId = borrowInfo.getUserId();

        QueryWrapper<Borrower> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Borrower borrower = borrowerMapper.selectOne(wrapper);

        BorrowerDetailVO borrowerDetailsVo = borrowerService.getBorrowerDetailsVoById(borrower.getId());

        map.put("borrower",borrowerDetailsVo);

        return map;


    }

    @Override
    public void approval(BorrowInfoApprovalVO borrowInfoApprovalVO) {
        //更新借款记录状态
        Long id = borrowInfoApprovalVO.getId();
        BorrowInfo borrowInfo = baseMapper.selectById(id);
        borrowInfo.setStatus(borrowInfoApprovalVO.getStatus());
        baseMapper.updateById(borrowInfo);

        if (borrowInfoApprovalVO.getStatus().intValue() == BorrowInfoStatusEnum.CHECK_OK.getStatus().intValue()) {
            //创建标的
            //TODO
            iLendService.createLend(borrowInfoApprovalVO,borrowInfo);
        }

    }
}
